package com.credit.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.credit.common.ServerResponse;
import com.credit.pojo.jurisdictionManagement;
import com.credit.pojo.personalQualifications;
import com.credit.pojo.user;
import com.credit.service.IUserService;
import com.credit.utlis.ImgUpload;
/**
 * 用户某块代码
 * @author Administrator
 *
 */
@CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
@RestController
@Controller
public class UserController {

	@Autowired
	private IUserService userService;
	
	
	/**
	 * 用户登录方法
	 * @param u
	 * @return
	 */
	@RequestMapping(value = "login.do", method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse login(user u) {
		
		return userService.login(u);
	}
	
	
	/**
	 * 修改用户信息
	 * @param u
	 * @return
	 */
	@RequestMapping(value = "upUser.do", method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse upUser(user u) {
		
		return userService.updateByPrimaryKeySelective(u);
	}
	
	
	/**
	 * 增加用户
	 * @param u
	 * @return
	 */
	@RequestMapping(value = "addUser.do", method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse addUser(user u) {
		
		
		return userService.insertSelective(u);
	}
	
	
	
	/**
	 * 修改用户权限
	 * @param j
	 * @return
	 */
	@RequestMapping(value = "upJurisdictionManagement.do", method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse upJurisdictionManagement(jurisdictionManagement j) {
		
		
		return userService.updateJurisdiction(j);
	}
	
	
	
	
	/**
	 * 查看所有权限
	 * @return
	 */
	@RequestMapping(value = "selectJurisdictionType.do", method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse selectJurisdictionType() {
		
		
		return userService.selectJurisdictionType();
	}
	
	
	/**
	 * 查看所有用户
	 * @param u
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value = "selectUser.do", method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse selectUser(user u,@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
			@RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
		
		
		return userService.selectUser(u, pageNum, pageSize);
	}
	
	
	/**
	 * 查看单个用户详细信息
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "selectOneUser.do", method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse selectOneUser(int id) {
		
		return userService.selectByPrimaryKey(id);
	}
	
	
	
	
	/**
	 * 查看个人资质
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "selectPersonalQualifications.do", method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse selectPersonalQualifications(int id) {
		
		return userService.selectPersonalQualifications(id);
	}
	
	
	/**
	 * 增加个人资质
	 * @param p
	 * @param photo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addPersonalQualifications.do", method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse addPersonalQualifications(personalQualifications p,@RequestParam(value = "file", required = false) MultipartFile photo,HttpServletRequest request) {
		try {
			String url = ImgUpload.PictureUpload(photo, request);
			p.setUrl(url);
		} catch (IOException e) {
			
			e.printStackTrace();
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return userService.insertPersonalQualifications(p);
	}
	
	
	/**
	 * 修改个人资质
	 * @param p
	 * @param photo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "upPersonalQualifications.do", method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse upPersonalQualifications(personalQualifications p,@RequestParam(value = "file", required = false) MultipartFile photo,HttpServletRequest request) {
		try {
			String url = ImgUpload.PictureUpload(photo, request);
			p.setUrl(url);
		} catch (IOException e) {
			
			e.printStackTrace();
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return userService.updatePersonalQualifications(p);
	}
	
	
	
	/**
	 * 删除个人资质
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "delPersonalQualifications.do", method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse delPersonalQualifications(int id) {
		
		return userService.deletePersonalQualifications(id);
	}
	
	
	
}
